package com.szqhyxj.youxiangju.biz.onlineChannel;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.format.Time;
import android.util.Log;
import android.util.TypedValue;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.szqhyxj.youxiangju.BaseActivity;
import com.szqhyxj.youxiangju.R;
import com.szqhyxj.youxiangju.biz.core.SharedPreferencesUtil;
import com.szqhyxj.youxiangju.biz.receipt.receiptdetail.ReceiptDetailActivity;
import com.szqhyxj.youxiangju.biz.web.BandingCardWebActivity;
import com.szqhyxj.youxiangju.biz.web.WebActivity;
import com.szqhyxj.youxiangju.biz.web.WebExit;
import com.szqhyxj.youxiangju.sys.components.NavigationBar;
import com.szqhyxj.youxiangju.sys.components.view.popview.ErrorPopView;
import com.szqhyxj.youxiangju.sys.components.view.popview.GainSMSPopView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 选择通道
 */
public class OnlineChannelListActivity extends BaseActivity implements NavigationBar.NavigationBarListener, OnlineChannelContract.View, ChannelAdapter.IconClickListener {
    private static final String CARD_NO = "cardNo";
    private static final String BANK_NAME = "bankname";
    private static final String AMOUNT = "amount";
    private static final String BTCODE = "btcode";
    private static final String TRADETYPE = "tradeType";
    private static final String CARDPHONE = "cardPhone";
    private static final String BILLDATE = "billDate";
    private static final String REPAYDATE = "repayDate";
    private static final String MERCHANTAREA = "merchantArea";
    @BindView(R.id.navigationBar)
    NavigationBar navigationBar;
    @BindView(R.id.channels)
    RecyclerView channels;
    @BindView(R.id.swipeLayout)
    SwipeRefreshLayout swipeLayout;
    @BindView(R.id.guider01)
    LinearLayout guider01;
    @BindView(R.id.guider02)
    LinearLayout guider02;
    @BindView(R.id.guider03)
    LinearLayout guider03;
    @BindView(R.id.guider04)
    LinearLayout guider04;
    @BindView(R.id.guider05)
    LinearLayout guider05;
    @BindView(R.id.guider06)
    LinearLayout guider06;
    @BindView(R.id.guider07)
    LinearLayout guider07;
    @BindView(R.id.none)
    LinearLayout none;
    @BindView(R.id.next03)
    TextView next03;
    @BindView(R.id.next04)
    TextView next04;
    @BindView(R.id.next05)
    TextView next05;
    @BindView(R.id.next06)
    TextView next06;
    @BindView(R.id.know01)
    TextView know01;
    private ChannelAdapter adapter;
    private List<ChannelModel.RowsItem> rowsItemList;

    private OnlineChannelPresenter presenter;
    private String cardNo;
    private String bankname;
    private String amount;
    private String btcode;
    private String tradeType;

    int pageSize = 20;
    int pageNumber = 1;
    String cardPhone;
    String billDate;
    String repayDate;
    String merchantArea;
    static OnlineChannelListActivity self;


    public static void start(Activity activity, String btCode, String bankname, String cardno, String amount, String tradeType, String cardPhone, String billDate, String repayDate, String merchantArea) {
        Intent intent = new Intent();
        intent.setClass(activity, OnlineChannelListActivity.class);
        intent.putExtra(BTCODE, btCode);
        intent.putExtra(BANK_NAME, bankname);
        intent.putExtra(CARD_NO, cardno);
        intent.putExtra(AMOUNT, amount);
        intent.putExtra(TRADETYPE, tradeType);
        intent.putExtra(CARDPHONE, cardPhone);
        intent.putExtra(BILLDATE, billDate);
        intent.putExtra(REPAYDATE, repayDate);
        intent.putExtra(MERCHANTAREA, merchantArea);
        activity.startActivity(intent);
    }

    @Override
    protected void onResume() {
        if (presenter==null){
            presenter = new OnlineChannelPresenter(this);
            presenter.initChannelList(btcode, tradeType);
        }else {
            presenter.initChannelList(btcode, tradeType);
        }
        super.onResume();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_online_channel_list);
        ButterKnife.bind(this);
        self = this;
        navigationBar.setNavigationBarListener(this);

        rowsItemList = new ArrayList<>();
        //adapter = new ChannelAdapter(rowsItemList, this);
        // channels.setAdapter(adapter);
        presenter = new OnlineChannelPresenter(this);
        cardNo = getIntent().getStringExtra(CARD_NO);
        bankname = getIntent().getStringExtra(BANK_NAME);
        amount = getIntent().getStringExtra(AMOUNT);
        btcode = getIntent().getStringExtra(BTCODE);
        tradeType = getIntent().getStringExtra(TRADETYPE);
        cardPhone = getIntent().getStringExtra(CARDPHONE);
        billDate = getIntent().getStringExtra(BILLDATE);
        repayDate = getIntent().getStringExtra(REPAYDATE);
        merchantArea = getIntent().getStringExtra(MERCHANTAREA);


        initEvent();
        Log.e("数据数据：","cardNo"+cardNo+"---"+"bankname"+bankname+"----amount"+amount+"----btcode"+btcode+"----tradeType"+tradeType+"----cardPhone"+cardPhone+"----billDate"+billDate+"----repayDate"+repayDate+"----merchantArea"+merchantArea);
        if (SharedPreferencesUtil.getOnlineChannelListShowShow()) {
            navigationBar.setVisibility(View.VISIBLE);
            swipeLayout.setVisibility(View.VISIBLE);
            guider01.setVisibility(View.GONE);
            guider02.setVisibility(View.GONE);
            guider03.setVisibility(View.GONE);
            guider04.setVisibility(View.GONE);
            guider05.setVisibility(View.GONE);
            guider06.setVisibility(View.GONE);
            guider07.setVisibility(View.GONE);
            presenter.initChannelList(btcode, tradeType);
            initViews();
        } else {
            navigationBar.setVisibility(View.GONE);
            swipeLayout.setVisibility(View.GONE);
            guider01.setVisibility(View.VISIBLE);
            guider02.setVisibility(View.GONE);
            guider03.setVisibility(View.GONE);
            guider04.setVisibility(View.GONE);
            guider05.setVisibility(View.GONE);
            guider06.setVisibility(View.GONE);
            guider07.setVisibility(View.GONE);

        }
    }

    private void initViews() {
        swipeLayout.setColorSchemeResources(R.color.colorPrimaryDark);
        swipeLayout.setProgressViewOffset(true, 0, (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 24, getResources().getDisplayMetrics()));
        swipeLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                presenter.initChannelList(btcode, tradeType);
                showRefresh(true);
            }
        });

    }

    @Override
    public void showRefresh(boolean isRefresh) {
        if (swipeLayout == null) {
            return;
        }
        swipeLayout.setRefreshing(isRefresh);
    }

    @Override
    public void showreceipt(ChannelModel.RowsItem rows) {
        if (rows.getAuthType() == 0) {
            GainSMSPopView gainSMSPopView = new GainSMSPopView(this, cardPhone,"已发送绑卡验证码至");
            gainSMSPopView.setOnClickListener(new GainSMSPopView.OnClickListener() {
                @Override
                public void onClickConfirm(String code) {

                }

                @Override
                public void onClickgetMsg() {
                    // presenter.getbindCard(rows, new Integer(btcode), 0, "0");
                }
            });
            gainSMSPopView.show();
        } else if (rows.getAuthType() == 1) {

        } else if (rows.getAuthType() == 2) {

        }

    }


    private void initEvent() {
     /*   channels.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {

            }
        });*/
    }

    @Override
    public void navigationLeft() {
        finish();
    }

    @Override
    public void navigationRight() {

    }

    @Override
    public void showChannelList(List<ChannelModel.RowsItem> data) {
        if (SharedPreferencesUtil.getOnlineChannelListShowShow()){
            navigationBar.setVisibility(View.VISIBLE);
            swipeLayout.setVisibility(View.VISIBLE);
            guider01.setVisibility(View.GONE);
            guider02.setVisibility(View.GONE);
            guider03.setVisibility(View.GONE);
            guider04.setVisibility(View.GONE);
            guider05.setVisibility(View.GONE);
            guider06.setVisibility(View.GONE);
            guider07.setVisibility(View.GONE);
            none.setVisibility(View.GONE);
            rowsItemList.clear();
            if (swipeLayout.isRefreshing()) {
                swipeLayout.setRefreshing(false);
            }
            rowsItemList.addAll(data);
            //   adapter.notifyDataSetChanged();


            if (data.size() == 0) {

            } else {

                channels.setOverScrollMode(View.OVER_SCROLL_NEVER);
                channels.setLayoutManager(new GridLayoutManager(this, 1));
                adapter = new ChannelAdapter(data, this);
                initListener();
                channels.setAdapter(adapter);

            }
        }else{
            navigationBar.setVisibility(View.GONE);
            none.setVisibility(View.GONE);
            swipeLayout.setVisibility(View.GONE);
            guider01.setVisibility(View.VISIBLE);
            guider02.setVisibility(View.GONE);
            guider03.setVisibility(View.GONE);
            guider04.setVisibility(View.GONE);
            guider05.setVisibility(View.GONE);
            guider06.setVisibility(View.GONE);
            guider07.setVisibility(View.GONE);
        }


    }

    private void initListener() {
        adapter.setOnRecyclerViewItemClickListener(new ChannelAdapter.OnItemClickListener() {
            @Override
            public void onClick(int position) {
                adapter.setThisPosition(position);
                adapter.notifyDataSetChanged();
            }

            @Override
            public void onLongClick(int position) {
            }
        });

    }

    GainSMSPopView gainSMSPopView;

    @Override
    public void onClickItem(int i, ChannelModel.RowsItem rows) {

        if (rows.getSupport() == 0) {
            Toast.makeText(this, "暂不支持", Toast.LENGTH_LONG).show();
        } else {

            if (rows.getBindStatus() == 0 || rows.getBindStatus() == 1 || rows.getBindStatus() == 2) {
            /*
            未绑定
             */
                /**
                 * 绑卡鉴权类型 0 验证码 1 链接 2 H5Code
                 */
                if (rows.getAuthType() == 0) {
                    gainSMSPopView = new GainSMSPopView(this, cardPhone,"已发送绑卡验证码至");
                    gainSMSPopView.setOnClickListener(new GainSMSPopView.OnClickListener() {
                        @Override
                        public void onClickConfirm(String code) {
                            if (code.equals("")){
                                Toast.makeText(OnlineChannelListActivity.this,"请输入验证码",Toast.LENGTH_LONG).show();
                            }else{
                                presenter.commitSms(code, btcode, rows.getChannelId() + "");
                            }

                        }

                        @Override
                        public void onClickgetMsg() {
                            presenter.getMessage(btcode, rows.getChannelId() + "", "", 0);

                        }
                    });
                    gainSMSPopView.show();
                } else if (rows.getAuthType() == 1) {
                    presenter.getMessage(btcode, rows.getChannelId() + "", "https://mobile.pay.szqhyxj.com/feedback", 1);

                } else if (rows.getAuthType() == 2) {
                    presenter.getMessage(btcode, rows.getChannelId() + "", "https://mobile.pay.szqhyxj.com/feedback", 2);
                }


            }
            if (rows.getBindStatus() == 3) {
                if (rows.getAuthType() == 0) {
                    Toast.makeText(this, "正在处理中，请稍等", Toast.LENGTH_LONG).show();

                } else {
                    presenter.getMessage(btcode, rows.getChannelId() + "", "http://www.szqhyxj.com/", 1);

                }
            }
            if (rows.getBindStatus() == 4) {
                /**
                 * 绑卡成功
                 */
                if (isCurrentInTimeScope(rows.getStartTime(), 0, rows.getEndTime(), 0)) {
                    if (tradeType.equals("1")) {
                        String title = bankname + "(" + cardNo.substring(cardNo.length() - 4, cardNo.length()) + ")";
                        Log.e("通道key",rows.getChannelCode());
                        WebActivity.startToH5(this, "createplan?type=online&&billDate=" + billDate + "&&repayDate=" + repayDate + "&&title=" + title + "&&channelKey=" + rows.getChannelCode() + "&&channelName=" + rows.getChannelName() + "&&merchantArea=" + merchantArea + "&&cardNo=" + cardNo, WebExit.PLAN_LIST.getCode());
                    }
                    if (tradeType.equals("2")) {

                        ReceiptDetailActivity.start(this, bankname, cardNo, amount, cardPhone, rows.getChannelCode() + "", btcode, "2", rows.getMinAmount() + "", rows.getMaxAmount() + "", rows.getStartTime(), rows.getEndTime());
                    }
                } else {
                    Toast.makeText(this, "此通道不在制定计划时间段内", Toast.LENGTH_LONG).show();
                }

            }
            if (rows.getBindStatus() == -1 || rows.getBindStatus() == -2) {
                ErrorPopView popView = new ErrorPopView(this, rows.getErrMsg());
                popView.show();
                if (rows.getAuthType() == 0) {
                    gainSMSPopView = new GainSMSPopView(this, cardPhone,"已发送绑卡验证码至");
                    gainSMSPopView.setOnClickListener(new GainSMSPopView.OnClickListener() {
                        @Override
                        public void onClickConfirm(String code) {
                            presenter.commitSms(code, btcode, rows.getChannelId() + "");
                        }

                        @Override
                        public void onClickgetMsg() {
                            presenter.getMessage(btcode, rows.getChannelId() + "", "", 0);

                        }
                    });

                    gainSMSPopView.show();
                } else if (rows.getAuthType() == 1) {
                    presenter.getMessage(btcode, rows.getChannelId() + "", "https://mobile.pay.szqhyxj.com/feedback", 1);

                } else if (rows.getAuthType() == 2) {
                    presenter.getMessage(btcode, rows.getChannelId() + "", "https://mobile.pay.szqhyxj.com/feedback", 2);
                }

            }

        }

    }

    //时间段判断
    public static boolean isCurrentInTimeScope(int beginHour, int beginMin, int endHour, int endMin) {
        boolean result = false;
        final long aDayInMillis = 1000 * 60 * 60 * 24;
        final long currentTimeMillis = System.currentTimeMillis();
        Time now = new Time();
        now.set(currentTimeMillis);
        Time startTime = new Time();
        startTime.set(currentTimeMillis);
        startTime.hour = beginHour;
        startTime.minute = beginMin;
        Time endTime = new Time();
        endTime.set(currentTimeMillis);
        endTime.hour = endHour;
        endTime.minute = endMin;
        if (!startTime.before(endTime)) {
            startTime.set(startTime.toMillis(true) - aDayInMillis);
            result = !now.before(startTime) && !now.after(endTime); // startTime <= now <= endTime
            Time startTimeInThisDay = new Time();
            startTimeInThisDay.set(startTime.toMillis(true) + aDayInMillis);
            if (!now.before(startTimeInThisDay)) {
                result = true;
            }
        } else {
            result = !now.before(startTime) && !now.after(endTime); // startTime <= now <= endTime
        }
        return result;
    }

    @Override
    public void enshowSMSview() {
        gainSMSPopView.dismiss();
        presenter.initChannelList(btcode, tradeType);
    }

    @Override
    public void showPage(String url) {
        BandingCardWebActivity.start(this, url);
    }

    @Override
    public void showNone() {
        if (SharedPreferencesUtil.getOnlineChannelListShowShow()){
            navigationBar.setVisibility(View.VISIBLE);
            none.setVisibility(View.VISIBLE);
            swipeLayout.setVisibility(View.GONE);
            guider01.setVisibility(View.GONE);
            guider02.setVisibility(View.GONE);
            guider03.setVisibility(View.GONE);
            guider04.setVisibility(View.GONE);
            guider05.setVisibility(View.GONE);
            guider06.setVisibility(View.GONE);
            guider07.setVisibility(View.GONE);
        }else {
            none.setVisibility(View.GONE);
            navigationBar.setVisibility(View.GONE);
            swipeLayout.setVisibility(View.GONE);
            guider01.setVisibility(View.VISIBLE);
            guider02.setVisibility(View.GONE);
            guider03.setVisibility(View.GONE);
            guider04.setVisibility(View.GONE);
            guider05.setVisibility(View.GONE);
            guider06.setVisibility(View.GONE);
            guider07.setVisibility(View.GONE);
        }

    }

    @OnClick({R.id.next01, R.id.next02, R.id.next03, R.id.next04, R.id.next05, R.id.next06, R.id.know01})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.next01:
                guider01.setVisibility(View.GONE);
                guider02.setVisibility(View.VISIBLE);
                guider03.setVisibility(View.GONE);
                guider04.setVisibility(View.GONE);
                guider05.setVisibility(View.GONE);
                guider06.setVisibility(View.GONE);
                guider07.setVisibility(View.GONE);
                break;
            case R.id.next02:
                guider01.setVisibility(View.GONE);
                guider02.setVisibility(View.GONE);
                guider03.setVisibility(View.VISIBLE);
                guider04.setVisibility(View.GONE);
                guider05.setVisibility(View.GONE);
                guider06.setVisibility(View.GONE);
                guider07.setVisibility(View.GONE);
                break;
            case R.id.next03:
                guider01.setVisibility(View.GONE);
                guider02.setVisibility(View.GONE);
                guider03.setVisibility(View.GONE);
                guider04.setVisibility(View.VISIBLE);
                guider05.setVisibility(View.GONE);
                guider06.setVisibility(View.GONE);
                guider07.setVisibility(View.GONE);
                break;
            case R.id.next04:
                guider01.setVisibility(View.GONE);
                guider02.setVisibility(View.GONE);
                guider03.setVisibility(View.GONE);
                guider04.setVisibility(View.GONE);
                guider05.setVisibility(View.VISIBLE);
                guider06.setVisibility(View.GONE);
                guider07.setVisibility(View.GONE);
                break;
            case R.id.next05:
                guider01.setVisibility(View.GONE);
                guider02.setVisibility(View.GONE);
                guider03.setVisibility(View.GONE);
                guider04.setVisibility(View.GONE);
                guider05.setVisibility(View.GONE);
                guider06.setVisibility(View.VISIBLE);
                guider07.setVisibility(View.GONE);
                break;
            case R.id.next06:
                guider01.setVisibility(View.GONE);
                guider02.setVisibility(View.GONE);
                guider03.setVisibility(View.GONE);
                guider04.setVisibility(View.GONE);
                guider05.setVisibility(View.GONE);
                guider06.setVisibility(View.GONE);
                guider07.setVisibility(View.VISIBLE);
                break;
            case R.id.know01:
                navigationBar.setVisibility(View.VISIBLE);
                swipeLayout.setVisibility(View.VISIBLE);
                guider07.setVisibility(View.GONE);
                SharedPreferencesUtil.setOnlineChannelListShowShow(true);
                presenter.initChannelList(btcode, tradeType);
                initViews();
                break;
        }
    }


}
